package com.wb.mas.app;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.wb.mas.data.DemoRepository;
import com.wb.mas.ui.auth.AuthBankViewModel;
import com.wb.mas.ui.auth.AuthContactViewModel;
import com.wb.mas.ui.auth.AuthExtraViewModel;
import com.wb.mas.ui.auth.AuthIdViewModel;
import com.wb.mas.ui.auth.AuthPIVIewModel;
import com.wb.mas.ui.auth.AuthWorkInfoViewModel;
import com.wb.mas.ui.bank.BankViewModel;
import com.wb.mas.ui.defer.DeferViewModel;
import com.wb.mas.ui.guide.GuideViewModel;
import com.wb.mas.ui.help.HelpViewModel;
import com.wb.mas.ui.login.LoginAccountModel;
import com.wb.mas.ui.login.LoginByPhoneViewModel;
import com.wb.mas.ui.login.LoginByPhoneViewModel2;
import com.wb.mas.ui.login.LoginViewModel;
import com.wb.mas.ui.main.MainViewModel;
import com.wb.mas.ui.order.OrderViewModel;
import com.wb.mas.ui.refund.RefundViewModel;
import com.wb.mas.ui.splash.SplashViewModel;
import com.wb.mas.ui.success.SuccessViewModel;
import com.wb.mas.ui.webview.WebViewViewModel;

/**
 * Created by goldze on 2019/3/26.
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final DemoRepository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, DemoRepository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(AuthExtraViewModel.class)) {
            return (T) new AuthExtraViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SplashViewModel.class)) {
            return (T) new SplashViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(GuideViewModel.class)) {
            return (T) new GuideViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(AuthIdViewModel.class)) {
            return (T) new AuthIdViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MainViewModel.class)) {
            return (T) new MainViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(BankViewModel.class)) {
            return (T) new BankViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(DeferViewModel.class)) {
            return (T) new DeferViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(HelpViewModel.class)) {
            return (T) new HelpViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(OrderViewModel.class)) {
            return (T) new OrderViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(RefundViewModel.class)) {
            return (T) new RefundViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(WebViewViewModel.class)) {
            return (T) new WebViewViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginAccountModel.class)) {
            return (T) new LoginAccountModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(AuthBankViewModel.class)) {
            return (T) new AuthBankViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(AuthContactViewModel.class)) {
            return (T) new AuthContactViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(AuthPIVIewModel.class)) {
            return (T) new AuthPIVIewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(AuthWorkInfoViewModel.class)) {
            return (T) new AuthWorkInfoViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginByPhoneViewModel.class)) {
            return (T) new LoginByPhoneViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginByPhoneViewModel2.class)) {
            return (T) new LoginByPhoneViewModel2(mApplication, mRepository);
        }else if (modelClass.isAssignableFrom(SuccessViewModel.class)) {
            return (T) new SuccessViewModel(mApplication, mRepository);
        }
//        if (modelClass.isAssignableFrom(NetWorkViewModel.class)) {
//            return (T) new NetWorkViewModel(mApplication, mRepository);
//        } else if (modelClass.isAssignableFrom(LoginViewModel.class)) {
//            return (T) new LoginViewModel(mApplication, mRepository);
//        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
